package com.wanmait.recycle.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.wanmait.recycle.pojo.Admin;
import com.wanmait.recycle.pojo.Firsttype;
import com.wanmait.recycle.pojo.Secondtype;
import com.wanmait.recycle.pojo.Unit;
import com.wanmait.recycle.service.FirsttypeService;
import com.wanmait.recycle.service.SecondtypeService;
import com.wanmait.recycle.util.UploadPhotosUtil;
@Controller
@RequestMapping("/manager")
public class SecondtypeController {
	@Resource
	private SecondtypeService secondtypeService;
	@Resource
	private FirsttypeService firsttypeService;
	@ModelAttribute("map")
	@RequestMapping("secondtype/secondtypeAdd")
	public Map<String,Object> secondtypeAddPage(){
		
		List<Firsttype> firsttypes=firsttypeService.findAll();
		
		Map<String, Object> infoMap = new HashMap<String,Object>();
		
		infoMap.put("firsttypes", firsttypes);
		
		return infoMap;
	}
	@RequestMapping("/secondtype/secondtypeAddOne")
	public String addSecondtype(String name,Integer firsttypeId,@RequestParam(value="file",required=false) MultipartFile file,HttpServletRequest request,String content)throws Exception {
		
		
		
		StringBuilder picsb=new StringBuilder();
		
			String photoName = UploadPhotosUtil.getPhotoNameAndSave("/static/rubbishType_img", file, request);
			picsb.append(photoName);
		
		String pic=picsb.toString();
		HttpSession session=request.getSession();
		Admin admin=(Admin)session.getAttribute("admin");
		secondtypeService.addSecondtype(name,firsttypeId,pic,content,admin);
		return "redirect:/manager/secondtype/secondtypeList";
	}
	
	@ModelAttribute("map")
	@RequestMapping("secondtype/secondtypeUpdate")
	public Map<String,Object> secondtypeUpdatePage(Integer id){
		
		List<Firsttype> firsttypes=firsttypeService.findAll();
		Secondtype secondtype=secondtypeService.findById(id);
		Map<String, Object> infoMap = new HashMap<String,Object>();
		
		infoMap.put("firsttypes", firsttypes);
		infoMap.put("secondtype",secondtype);
		
		return infoMap;
	}
	@RequestMapping("/secondtype/secondtypeUpdateOne")
	public String updateSecondtype(String id,String name,String firsttypeId,@RequestParam(value="file",required=false) MultipartFile file,HttpServletRequest request,String content)throws Exception {
		Integer nid=Integer.parseInt(id);
		Integer nfirsttypeId=Integer.parseInt(firsttypeId);
		
		
		StringBuilder picsb=new StringBuilder();
		
		String photoName = UploadPhotosUtil.getPhotoNameAndSave("/static/rubbishType_img", file, request);
		picsb.append(photoName);
		
		String pic=picsb.toString();
		HttpSession session=request.getSession();
		Admin admin=(Admin)session.getAttribute("admin");
		secondtypeService.updateSecondtypeById(nid,name,nfirsttypeId,pic,content,admin);
		return "redirect:/manager/secondtype/secondtypeList";
	}
	
	@RequestMapping("secondtype/secondtypeList")
	public ModelAndView rubbishList(){
		
		PageInfo<Secondtype> pageInfo= secondtypeService.findAllByPager();
		
		ModelAndView mv=new ModelAndView();
		mv.addObject("pageInfo",pageInfo);
		
		return mv;
	}
	@RequestMapping("secondtype/secondtypeSearch")
	public ModelAndView searchUserinfo(String searchStr, String beginTime, String endTime,String pageNum,String ftId) {
		if(ftId!=null&&!("".equals(ftId))) {
			Integer nftId=Integer.parseInt(ftId);
			ModelAndView mv=new ModelAndView("/manager/secondtype/secondtypeSearchII");
			Map<String,String> searchMap=new HashMap<String,String>();
			mv.addObject("pageInfo", secondtypeService.findAllByPrimarykey(nftId));
			mv.addObject("ftId",ftId);
			searchMap.put("searchStr",searchStr);
			searchMap.put("beginTime",beginTime);
			searchMap.put("endTime",endTime);
			mv.addObject("searchMap",searchMap);
			return mv;
		}
		 if ("".equals(searchStr)) { 
			 searchStr = null;
		 } 
		 if ("".equals(beginTime)) {
			 beginTime = null;
		 } 
		 if ("".equals(endTime)) { 
			 endTime = null; 
		 }
		
		ModelAndView mv=new ModelAndView();
		if(searchStr==null&&beginTime==null&&endTime==null&&pageNum==null) {
			return new ModelAndView("redirect:/manager/secondtype/secondtypeList");
		}
		mv.addObject("pageInfo", secondtypeService.findAllBySearch(searchStr, beginTime, endTime));
		Map<String,String> searchMap=new HashMap<String,String>();
		searchMap.put("searchStr",searchStr);
		searchMap.put("beginTime",beginTime);
		searchMap.put("endTime",endTime);
		mv.addObject("searchMap",searchMap);
		return mv;
		
	}	
	@RequestMapping("secondtype/secondtypeUpLoad")
	public String secondtypeAddUpLoad(Secondtype secondtype){
		secondtypeService.insert(secondtype);
		return "redirect:/manager/secondtype/secondtypeList";
	}
	
	
	
	
	@RequestMapping("secondtype/secondtypeChange")
	public String rubbishChange(Secondtype secondtype){
		secondtypeService.update(secondtype);
		return "redirect:/manager/secondtype/secondtypeList";
	}
	
	@RequestMapping("secondtype/secondTypeChangeUpLoad")
	public String rubbishChangeUpLoad(Secondtype secondtype){
		secondtypeService.update(secondtype);
	
		return "redirect:/manager/secondtype/secondtypeList";
		
	}
	
@RequestMapping("secondtype/deleteSelect")
@ResponseBody
	public void secondtypeDelete(String ids){
		
		secondtypeService.deleteByIds(ids);
		
	}
@RequestMapping("secondtype/deleteOne")
@ResponseBody
	public void secondtypeDelete(Integer id){
		
		secondtypeService.deleteById(id);
		
	}
	
@RequestMapping("secondtype/recallOne")
@ResponseBody
	public void secondtypeRecall(Integer id){
		
		secondtypeService.recallById(id);
		
	}

	@RequestMapping("secondtype/changeSortCount")
	@ResponseBody
	public void changeSortCount(Integer Tid,Integer Tcount,Integer id, Integer count){
		
		secondtypeService.changeSortCount(Tid,count);
		secondtypeService.changeSortCount(id,Tcount);
		
	}


	
}
